/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ipartek.formacion.gestformwww.controlador;

import com.ipartek.formacion.gestformwww.modelo.ModeloContacto;
import com.ipartek.formacion.gestformwww.modelo.beans.Contacto;
import com.ipartek.formacion.gestformwww.modelo.beans.excepciones.GestorFormacionExcepcion;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Metodo ModificarContactoAccion que lo que realiza es modificar un contacto e insertarlo en la base de
 * datos
 * @author Jorge y Enrique
 */
public class ModificarContactoAccion implements Accion{

    public AccionRouter ejecuta(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) {
        Date d = null;
        SimpleDateFormat sdf;
        Timestamp ts;
        AccionRouter router = null;
        HttpSession sesion = request.getSession();
        AccionServlet as=(AccionServlet) servlet;
        ModeloContacto modelo = (ModeloContacto) servlet.getServletContext().getAttribute("ModeloContacto");
        Contacto contacto=new Contacto();

        if (sesion.getAttribute("usuario") == null) {
            router = new AccionRouter("inicio", false);
        } else {
            sdf =new SimpleDateFormat("dd/MM/yyyy");
              try {
             d = sdf.parse(request.getParameter("FechaContacto"));
        } catch (ParseException ex) {
            Logger.getLogger(CrearContactoAccion.class.getName()).log(Level.SEVERE, null, ex);
        }
            ts=new Timestamp(d.getTime());
        // <editor-fold defaultstate="collapsed" desc="Rellena el bean de alumno">
                contacto.setCanId(Integer.parseInt(request.getParameter("CanId")));
                contacto.setConvId(Integer.parseInt(request.getParameter("ConvId")));
                contacto.setFechaCon (ts);
                contacto.setObs(request.getParameter("Observaciones"));
            try {
                modelo.modificar(contacto);
            } catch (GestorFormacionExcepcion ex) {
                sesion.setAttribute("excepciones", ex.getMessage());
                Logger.getLogger(ModificarContactoAccion.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                router = as.getFactory().getAction(as.getNombreClaseRecurso("listar-contacto-accion"), servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(CrearContactoAccion.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(CrearContactoAccion.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(CrearContactoAccion.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
         return router;
}}



